package core

import (
	"fmt"
	partner "gitee.com/binny_w/gin-partner"
	"gitee.com/binny_w/go-util"
	"github.com/gin-gonic/gin"
	"github.com/shirou/gopsutil/v4/host"
	"net/http"
	"runtime"
	"strings"
	"sync"
	"time"
)

var WaitCore = sync.WaitGroup{}
var StartAt = time.Now().Unix()

func init() {
	WaitCore.Add(1)
	defer func() {
		WaitCore.Done()
		var m runtime.MemStats
		runtime.ReadMemStats(&m)
		hInfo := "未能获取"
		if h, err := host.Info(); err == nil {
			hInfo = fmt.Sprintf("%s @ %s(%s) %s / %s", h.Hostname, h.Platform, h.PlatformFamily, h.PlatformVersion, h.KernelArch)
		}
		Engine.RegisterRouter(ApiGroup, http.MethodGet, "/system/info", func(c *gin.Context) {
			partner.WriteJsonSuccess(c, gin.H{
				"run_mode":      gin.Mode(),
				"server_start":  StartAt,
				"server_time":   time.Now().Unix(),
				"version_go":    strings.TrimLeft(runtime.Version(), "go"),
				"version_gin":   strings.TrimLeft(gin.Version, "v"),
				"version_mysql": MysqlVersion,
				"version_redis": RedisVersion,
				"os_info":       hInfo,
				"mem_alloc":     util.StrNiceSize(int64(m.Alloc)),
			})
		})
	}()
	configInit()
	loggerInit()
	mysqlInit()
	tablesInit()
	redisInit()
	ossInit()
	rbacInit()
	ip2regionInit()
	cacheInit()
	engineInit()
}
